using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.Common;
using PluginHandler.UI;

namespace MySqlPlugin.UI
{
    internal partial class ConnectionDialog : UserControl, IConnectionDialog
    {
        private DbConnectionStringBuilder _connectionStringBuilder;

        private const int _defaultPort = 3306;

        public ConnectionDialog()
        {
            InitializeComponent();

            numPort.Value = _defaultPort;
        }

        #region IConnectionDialog Member

        public DbConnectionStringBuilder ConnectionStringDetails
        {
            get 
            {
                if (_connectionStringBuilder == null)
                    _connectionStringBuilder = new DbConnectionStringBuilder();

                _connectionStringBuilder["Server"] = txtServer.Text;
                _connectionStringBuilder["Database"] = txtDatabase.Text;
                _connectionStringBuilder["Port"] = chkNamedPipes.Checked ? -1 : numPort.Value;
                _connectionStringBuilder["Uid"] = txtUsername.Text;
                _connectionStringBuilder["Pwd"] = txtPassword.Text;

                return _connectionStringBuilder;
            }
            set 
            {
                _connectionStringBuilder = value;

                int port;
                bool isUsingNamedPipes = false;

                if (!int.TryParse(_connectionStringBuilder["Port"].ToString(), out port))
                {
                    isUsingNamedPipes = true;
                    port = _defaultPort;
                }

                txtServer.Text = _connectionStringBuilder["Server"].ToString();
                txtDatabase.Text = _connectionStringBuilder["Database"].ToString();
                numPort.Value = port;
                chkNamedPipes.Checked = isUsingNamedPipes;
                txtUsername.Text = _connectionStringBuilder["Uid"].ToString();
                txtPassword.Text = TryGetValue(_connectionStringBuilder, "Pwd");
            }
        }

        public bool FilledComplete
        {
            get
            {
                bool filledComplete = false;

                filledComplete =
                    txtServer.Text.Trim().Length > 0 &&
                    txtDatabase.Text.Trim().Length > 0 &&
                    txtUsername.Text.Trim().Length > 0;

                return filledComplete;
            }
        }

        public bool AskForPassword
        {
            get { return chkAskForPassword.Checked; }
            set { chkAskForPassword.Checked = value; }
        }
        #endregion

        private string TryGetValue(DbConnectionStringBuilder dbConnectionStringBuilder, string keyword)
        {
            if (dbConnectionStringBuilder.ContainsKey(keyword))
            {
                return dbConnectionStringBuilder[keyword].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
